<?php
/**
 * Created by PhpStorm.
 * User: papa
 * Date: 18/5/21
 * Time: 17:44
 */

namespace app\api\model;


class CouponTickets extends BaseModel
{

    protected $hidden = [''];

    public function getIsUseAttr($value)
    {
        $status = [0=>'未使用',1=>'已使用',2=>'已过期'];
        return $status[$value];
    }
    
    //关联优惠劵
    public function couponCate()
    {
        return $this->belongsTo('CouponCategory','coupon_category_id','id');
    }

    //关联活动表
    public function activity()
    {
        return $this->belongsTo('Activity','activity_id','id');
    }

    //获取用户可用优惠劵
    public static function useCoupon($uid)
    {
        //获取现在时间
        $time = (new \DateTime())->format('Y-m-d H:i:s');
        //自己可用，并且关联活动条件
        $coupon = self::where('user_id',$uid)
            ->with('couponCate')
            ->where('is_use',0)
            ->with(['activity' => function($query) use($time){
                $query->whereTime('end_time','>=',$time);
            }])
            ->select();
        //如果关联条件位null,则活动已过期，销毁处理
        $arr = [];
        foreach ($coupon as $key => &$value) {
            if(!$value->activity){
                unset($coupon[$key]);
            }else{
                $arr[$key] = $value->coupon_cate;
                $arr[$key]['end_time'] = $value->activity->end_time;
            }
        }
        return array_values($arr);
    }
    
    //查询当前优惠劵是否可用
    public static function checkCoupon($id,$uid)
    {
        $time = (new \DateTime())->format('Y-m-d H:i:s');
        $check =self::where(['user_id'=>$uid,'coupon_category_id'=>$id,'is_use'=>0])
            ->with(['activity' => function($query) use($time){
                $query->whereTime('end_time','>=',$time);
            },'couponCate'])
            ->find();
        //查询是否有优惠劵
        if(!$check){
            return false;
        }//检测活动是否过期
        if($check->activity){
            return $check;
        }else{
            return false;
        }
    }



    //返回用户所有可用的优惠券（张亚博）
    public static function getUserAvailableCoupon($uid)
    {
        $coupon = self::where(['user_id'=>$uid,'is_use'=>0])->select();
        $coupons = [];
        $now = date('Y-m-d H:i:s');
        foreach($coupon as $k=>$v){
            $time = Activity::where(['id'=>$v->getData('activity_id')])->find();
            if($now > $time->getData('end_time')){
                self::where(['id'=>$v->getData('id')])->update(['is_use'=>2]);
            }else if($now < $time->getData('start_time')){
                //什么也不做
            }else{
                $coupons[] = $v->getData('id');
            }
        }
        return $coupons;
    }


    //检测某个优惠券是否可用（张亚博）
    public static function checkForCoupon($cid,$uid)
    {
        $coupon = self::where(['id'=>$cid,'user_id'=>$uid])->find();
        if(!$coupon){
            return false;
        }
        if($coupon->is_use != 0){
            return false;
        }
        $time = Activity::find($coupon->getData('activity_id'));
        $now = date('Y-m-d H:i:s');
        if($now > $time->getData('end_time')){
            self::where(['id'=>$coupon->getData('id')])->update(['is_use'=>2]);
            return false;
        }else if($now < $time->getData('start_time')){
            return false;
        }else{
            $arr = [];
            $couponCategory = CouponCategory::where('id','eq',$coupon->getData('coupon_category_id'))->find();
            if($couponCategory->getData('coupon_cate_cate') == 1){
                $arr['id'] = $coupon->getData('id');
                $arr['coupon_cate'] = $couponCategory->getData('coupon_cate_cate');
                $arr['low_price'] = $couponCategory->getData('low_price');
                $arr['amount_cash'] = $couponCategory->getData('amount_cash');
                return $arr;
            }elseif($couponCategory->getData('coupon_cate_cate') == 2){
                $arr['id'] = $coupon->getData('id');
                $arr['coupon_cate'] = $couponCategory->getData('coupon_cate_cate');
                $arr['discount_ticket'] = $couponCategory->getData('discount_ticket');
                return $arr;
            }else{
                return false;
            }
        }
    }
}